#! _*_ coding: utf8 _*_
from django.contrib.auth.models import User

from .models import Student


def student_of_user(user, create=True):
    """返回user对应的student, 如果不存在并且create为真，则创建新的student"""

    if hasattr(user, 'student'):
        return user.student
    else:
        if create:
            s = Student.objects.create(me=user)
            return s
        else:
            return None


def student_of_uid(uid, create=True):
    try:
        u = User.objects.get(pk=uid)
        return student_of_user(u, create)
    except User.DoesNotExist:
        return None
